﻿#include "page9.h"
#include "databaseManager.h"
#include <QHeaderView>
#include <QFont>
#include <QMessageBox>
#include "AddInventoryDialog.h"
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlRecord>

Page9::Page9(QWidget* parent) : QWidget(parent)
{
    // 设置窗口大小
    setFixedSize(1280, 720);
    // 设置背景
    QPixmap background(":new/prefix1/bg.jpg");
    QLabel* backgroundLabel = new QLabel(this);
    backgroundLabel->setPixmap(background.scaled(this->size())); // 缩放背景图片以适应窗口大小
    backgroundLabel->setGeometry(0, 0, this->width(), this->height());
    backgroundLabel->lower();

    QPixmap overlayImage(":new/prefix1/newbg.jpg");
            QLabel* overlayLabel = new QLabel(this);
            overlayLabel->setPixmap(overlayImage.scaled(1020, 590)); // 缩放新图片为200x150
            overlayLabel->setGeometry(265, 135, 1020, 590); // 设置新图片的位置和大小

    // 设置按钮的初始Y轴位置
    int yPos = 222;

    // Assuming you have a QIcon object created from your image file
    QIcon icon1(":new/prefix1/order.jpg");
    QIcon icon11(":new/prefix1/order2.jpg");
    // Create the QPushButton and set its properties
    orderButton = new QPushButton(this);
    orderButton->setIcon(icon1);  // Set the icon
    orderButton->setIconSize(QSize(260, 71));  // Set the icon size
    orderButton->setFlat(true);  // Make the button flat (no background)
    orderButton->setFixedSize(260, 71);  // Set the fixed size of the button
    orderButton->setGeometry(1, yPos, 260, 71);
    connect(orderButton, &QPushButton::clicked, this, [this](){
        emit orderRequested();
    });
    connect(orderButton, &QPushButton::pressed, this, [this, icon11]() {
        orderButton->setIcon(icon11);  // Change to icon10 when pressed
    });

    connect(orderButton, &QPushButton::released, this, [this, icon1]() {
        orderButton->setIcon(icon1);  // Revert to icon1 when released
        emit orderRequested();  // Emit the signal after the button is released
    });

    // 调整下一个按钮的Y轴位置
    yPos += 68;

    // 创建并设置生产按钮
    QIcon icon2(":new/prefix1/production.jpg");
    QIcon icon12(":new/prefix1/production2.jpg");
    productionButton = new QPushButton("", this);
    productionButton->setIcon(icon2);  // Set the icon
    productionButton->setIconSize(QSize(260, 71));  // Set the icon size
    productionButton->setFlat(true);  // Make the button flat (no background)
    productionButton->setFixedSize(260, 71);  // Set the fixed size of the button
    productionButton->setGeometry(1, yPos, 260, 71);
    connect(productionButton, &QPushButton::clicked, this, [this]() {
        emit productionRequested();
    });
    connect(productionButton, &QPushButton::pressed, this, [this, icon12]() {
        productionButton->setIcon(icon12);
    });

    connect(productionButton, &QPushButton::released, this, [this, icon2]() {
        productionButton->setIcon(icon2);
        emit productionRequested();  // Emit the signal after the button is released
    });

    // 调整下一个按钮的Y轴位置
    yPos += 68;

    // 创建并设置财务按钮
    QIcon icon3(":new/prefix1/finance.jpg");
    QIcon icon13(":new/prefix1/finance2.jpg");
    financeButton = new QPushButton("", this);
    financeButton->setIcon(icon3);  // Set the icon
    financeButton->setIconSize(QSize(260, 71));  // Set the icon size
    financeButton->setFlat(true);  // Make the button flat (no background)
    financeButton->setFixedSize(260, 71);  // Set the fixed size of the button
    financeButton->setGeometry(1, yPos, 260, 71);
    connect(financeButton, &QPushButton::clicked, this, [this]() {
        emit financeRequested();
    });
    connect(financeButton, &QPushButton::pressed, this, [this, icon13]() {
        financeButton->setIcon(icon13);
    });

    connect(financeButton, &QPushButton::released, this, [this, icon3]() {
        financeButton->setIcon(icon3);
        emit financeRequested();  // Emit the signal after the button is released
    });


    // 调整下一个按钮的Y轴位置
    yPos += 68;

    // 创建并设置库存按钮
    QIcon icon4(":new/prefix1/warehouse.jpg");
    QIcon icon14(":new/prefix1/warehouse2.jpg");
    warehouseButton = new QPushButton("", this);
    warehouseButton->setIcon(icon14);  // Set the icon
    warehouseButton->setIconSize(QSize(260, 71));  // Set the icon size
    warehouseButton->setFlat(true);  // Make the button flat (no background)
    warehouseButton->setFixedSize(260, 71);  // Set the fixed size of the button
    warehouseButton->setGeometry(1, yPos, 260, 71);
    connect(warehouseButton, &QPushButton::clicked, this, [this]() {
        emit storeRequested();
    });

    connect(warehouseButton, &QPushButton::pressed, this, [this, icon4]() {
        warehouseButton->setIcon(icon4);
    });

    connect(warehouseButton, &QPushButton::released, this, [this, icon14]() {
        warehouseButton->setIcon(icon14);
        emit storeRequested();  // Emit the signal after the button is released
    });

    // 创建返回按钮
        frontButton = new QPushButton(u8"", this);
        frontButton->setGeometry(1050,0,170,55); // 调整此位置以适应屏幕
        frontButton->setStyleSheet("QPushButton {"
                                 " border: none; "
                                 "background-image: url(:new/prefix1/tuichudenglu.jpg); "
                                 "background-repeat: no-repeat; "
                                 "background-position: center; }");


        connect(frontButton, &QPushButton::pressed, this, [this]() {
            frontButton->setStyleSheet("QPushButton {"
                                     " border: none; "
                                     "background-image: url(:/new/prefix1/tuichudenglu2.jpg); "
                                     "background-repeat: no-repeat; "
                                     "background-position: center; }");
        });
        connect(frontButton, &QPushButton::released, this, [this]() {
            frontButton->setStyleSheet("QPushButton {"
                                     " border: none; "
                                     "background-image: url(:/new/prefix1/tuichudenglu.jpg); "
                                     "background-repeat: no-repeat; "
                                     "background-position: center; }");
        });

   connect(frontButton, &QPushButton::clicked, this, &Page9::onFrontButtonClicked);
   QVBoxLayout* backLayout = new QVBoxLayout(this);
   setLayout(backLayout);


    //用户指南
        yPos += 68;
    QIcon icon6(":new/prefix1/operationGuide.jpg");
    QIcon icon16(":new/prefix1/operationGuide2.jpg");
    helpButton = new QPushButton("", this);
    helpButton->setIcon(icon6);  // Set the icon
    helpButton->setIconSize(QSize(260, 71));  // Set the icon size
    helpButton->setFlat(true);  // Make the button flat (no background)
    helpButton->setFixedSize(260, 71);  // Set the fixed size of the button
    helpButton->setGeometry(1, yPos, 260, 71);

    connect(helpButton, &QPushButton::clicked, this, [this]() {
        emit pageRequested10();
    });

    connect(helpButton, &QPushButton::pressed, this, [this, icon16]() {
        helpButton->setIcon(icon16);
    });
    connect(helpButton, &QPushButton::released, this, [this, icon6]() {
        helpButton->setIcon(icon6);
        emit pageRequested10();  // Emit the signal after the button is released
    });

    //连接按钮
         yPos += 68;
        QIcon icon7(":new/prefix1/connect.jpg");
        QIcon icon17(":new/prefix1/connect2.jpg");
        connectButton = new QPushButton("", this);
        connectButton->setIcon(icon7);  // Set the icon
        connectButton->setIconSize(QSize(260, 71));  // Set the icon size
        connectButton->setFlat(true);  // Make the button flat (no background)
        connectButton->setFixedSize(260, 71);  // Set the fixed size of the button
        connectButton->setGeometry(1, yPos, 260, 71);

        connect(connectButton, &QPushButton::clicked, this, [this]() {
            emit pageRequested13();
        });

        connect(connectButton, &QPushButton::pressed, this, [this, icon17]() {
             connectButton->setIcon(icon17);
        });
        connect(connectButton, &QPushButton::released, this, [this, icon7]() {
            connectButton->setIcon(icon7);
            emit pageRequested13();  // Emit the signal after the button is released
        });


        // 创建并设置添加员工按钮
        yPos += 68;
            QIcon icon5(":new/prefix1/tianjiayuangong01.jpg");
            QIcon icon15(":new/prefix1/tianjiayuangong02.jpg");
            addEmployeeButton = new QPushButton("", this);
            addEmployeeButton->setIcon(icon5);  // Set the icon
            addEmployeeButton->setIconSize(QSize(260, 71));  // Set the icon size
            addEmployeeButton->setFlat(true);  // Make the button flat (no background)
            addEmployeeButton->setFixedSize(260, 71);  // Set the fixed size of the button
            addEmployeeButton->setGeometry(1, yPos, 260, 71);

            connect(addEmployeeButton, &QPushButton::clicked, this, [this]() {
                emit addEmployeeRequested();
            });

            connect(addEmployeeButton, &QPushButton::pressed, this, [this, icon5]() {
                addEmployeeButton->setIcon(icon5);
            });

            connect(addEmployeeButton, &QPushButton::released, this, [this, icon15]() {
                addEmployeeButton->setIcon(icon15);
                emit addEmployeeRequested();  // Emit the signal after the button is released
            });

    inventoryButton = new QPushButton(u8"", this);
    inventoryButton->setGeometry(265, 60, 264, 75); // 调整此位置以适应屏幕
    inventoryButton->setStyleSheet("QPushButton {"
                                   " border: none; "
                                   "background-image: url(:new/prefix1/kucun.jpg); "
                                   "background-repeat: no-repeat; "
                                   "background-position: center; }");
    // Connect pressed signal to change the background image to shouzhibiao2.jpg
    connect(inventoryButton, &QPushButton::pressed, this, [this]() {
        inventoryButton->setStyleSheet("QPushButton {"
                                       " border: none; "
                                       "background-image: url(:/new/prefix1/kucun2.jpg); "
                                       "background-repeat: no-repeat; "
                                       "background-position: center; }");
    });

    // Connect released signal to change the background image back to shouzhibiao.jpg
    connect(inventoryButton, &QPushButton::released, this, [this]() {
        inventoryButton->setStyleSheet("QPushButton {"
                                       " border: none; "
                                       "background-image: url(:/new/prefix1/kucun.jpg); "
                                       "background-repeat: no-repeat; "
                                       "background-position: center; }");
    });

    purchaseButton = new QPushButton(u8"", this);
    purchaseButton->setGeometry(530, 60, 264, 75); // 调整此位置以适应屏幕
    purchaseButton->setStyleSheet("QPushButton {"
                                  " border: none; "
                                  "background-image: url(:new/prefix1/ruku.jpg); "
                                  "background-repeat: no-repeat; "
                                  "background-position: center; }");    // Connect pressed signal to change the background image to shouzhibiao2.jpg
    connect(purchaseButton, &QPushButton::pressed, this, [this]() {
        purchaseButton->setStyleSheet("QPushButton {"
                                      " border: none; "
                                      "background-image: url(:/new/prefix1/ruku2.jpg); "
                                      "background-repeat: no-repeat; "
                                      "background-position: center; }");
    });

    // Connect released signal to change the background image back to shouzhibiao.jpg
    connect(purchaseButton, &QPushButton::released, this, [this]() {
        purchaseButton->setStyleSheet("QPushButton {"
                                      " border: none; "
                                      "background-image: url(:/new/prefix1/ruku.jpg); "
                                      "background-repeat: no-repeat; "
                                      "background-position: center; }");
    });

    queryButton = new QPushButton(u8"", this);
    queryButton->setGeometry(795, 60, 264, 75); // 调整此位置以适应屏幕
    queryButton->setStyleSheet("QPushButton {"
                               " border: none; "
                               "background-image: url(:new/prefix1/chaxun.jpg); "
                               "background-repeat: no-repeat; "
                               "background-position: center; }");
    // Connect pressed signal to change the background image to shouzhibiao2.jpg
    connect(queryButton, &QPushButton::pressed, this, [this]() {
        queryButton->setStyleSheet("QPushButton {"
                                   " border: none; "
                                   "background-image: url(:/new/prefix1/chaxun2.jpg); "
                                   "background-repeat: no-repeat; "
                                   "background-position: center; }");
    });

    // Connect released signal to change the background image back to shouzhibiao.jpg
    connect(queryButton, &QPushButton::released, this, [this]() {
        queryButton->setStyleSheet("QPushButton {"
                                   " border: none; "
                                   "background-image: url(:/new/prefix1/chaxun.jpg); "
                                   "background-repeat: no-repeat; "
                                   "background-position: center; }");
    });

    // 连接按钮的信号和槽
    connect(inventoryButton, &QPushButton::clicked, this, [this]() {
        emit pageRequested7();
    });
    connect(purchaseButton, &QPushButton::clicked, this, [this]() {
        openAddInventoryDialog();
        emit pageRequested7();
    });
    connect(queryButton, &QPushButton::clicked, this, [this]() {
        emit pageRequested9();
    });

    yPos += 68;



    setupCentralWidgets();
}

void Page9::setupCentralWidgets()
{
    setStyleSheet(
        "QLineEdit, QDateEdit {"
        "  font-size: 18px;"
        "  font-weight: bold;"
        "  border: 2px solid #E0E0E0;"
        "  border-radius: 20px;"
        "  padding: 10px;"
        "  color: black;"
        "}"
        "QComboBox {"
        "  font-size: 18px;"
        "  font-weight: bold;"
        "  border: 2px solid #E0E0E0;"
        "  border-radius: 20px;"
        "  padding: 10px;"
        "  color: black;"
        "}"
    );

    // 设置标签和编辑框的字体大小
    QFont labelFont;
    labelFont.setPointSize(24);

    QFont editFont;
    editFont.setPointSize(24);

    // 创建种类下拉框和标签
        QLabel* categoryLabel = new QLabel(u8"种类", this);
        categoryLabel->setStyleSheet("font-size: 18px; font-weight: bold;");
        categoryLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
        categoryLabel->setGeometry(400, 260, 150, 50);

        categoryCombo = new QComboBox(this);
        categoryCombo->setFont(editFont);
        categoryCombo->setFixedSize(400, 50);
        categoryCombo->setGeometry(560, 260, 400, 50);

        // 添加 A-Z 26 个种类到下拉框
        for (char c = 'A'; c <= 'Z'; ++c) {
            categoryCombo->addItem(QString(c));
        }

        // 创建数量编辑框和标签
        QLabel* quantityLabel = new QLabel(u8"数量", this);
        quantityLabel->setStyleSheet("font-size: 18px; font-weight: bold;");
        quantityLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
        quantityLabel->setGeometry(400, 340, 150, 50);

        quantityEdit = new QLineEdit(this);
        quantityEdit->setFont(editFont);
        quantityEdit->setFixedSize(400, 50);
        quantityEdit->setGeometry(560, 340, 400, 50);

        // 创建并设置“确定”按钮
        confirmButton = new QPushButton(u8"确定", this);
        confirmButton->setFont(labelFont);
        confirmButton->setGeometry(600, 420, 300, 50);
        confirmButton->setStyleSheet("QPushButton {"
                                     "  font-size: 18px;"
                                     "  font-weight: bold;"
                                     "  border: 2px solid #E0E0E0;"
                                     "  border-radius: 20px;"
                                     "  padding: 10px;"
                                     "  background-color: #0093D1;"
                                     "  color: white;"
                                     "}");

    // 连接确认按钮的点击信号到槽函数
    connect(confirmButton, &QPushButton::clicked, this, &Page9::onConfirmButtonClicked);
}

void Page9::onConfirmButtonClicked()
{
    QString category = categoryCombo->currentText();
    DatabaseManager &dbManager = DatabaseManager::instance();
    QSqlTableModel* model = dbManager.getInventoryByType(category);

    if (model->rowCount() > 0) {
        QModelIndex index = model->index(0, 2);  // 假设数量在第3列
        QString quantity = model->data(index).toString();
        quantityEdit->setText(quantity);
    } else {
        QMessageBox::warning(this, "注意！", "库存不存在该品种！");
    }

    delete model;  // 清理QSqlTableModel对象
}

void Page9::financeHide()
{
    financeButton->hide();
}

void Page9::showEvent(QShowEvent* event)
{
    QWidget::showEvent(event);
}

void Page9::openAddInventoryDialog() {
    AddInventoryDialog dialog(this);
    if (dialog.exec() == QDialog::Accepted) {
        // Use the getter functions to obtain the values from the dialog
        QString rawMaterialType = dialog.getKindComboBox()->currentText();
        int amount = dialog.getQuantityInput()->text().toInt();
        int price = dialog.getPriceInput()->text().toInt();
        QString date = dialog.getDateEdit()->date().toString("yyyy-MM-dd");
        int totalprice=price*amount;
        // 插入库存到数据库
        if (DatabaseManager::instance().changeInventory(rawMaterialType, amount)&&DatabaseManager::instance().insertEnterRecord(date,rawMaterialType,amount,price,totalprice)) {
            QMessageBox::information(this, u8"成功", u8"库存已成功添加到数据库");
        } else {
            QMessageBox::warning(this, u8"失败", u8"添加库存到数据库失败");
        }
    }
}

void Page9::signuphide() {
    addEmployeeButton->hide();
}
void Page9::onFrontButtonClicked()
{
    QMessageBox msgBox;
    msgBox.setText("确定退出登录吗?");
    msgBox. setWindowTitle("退出登录");
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.button(QMessageBox::Ok)->setText("确认");
    msgBox.button(QMessageBox::Cancel)->setText("取消");
    msgBox.setStyleSheet(
        "QMessageBox {"
        "  background-color: #FFFFFF;"
        "}"
        "QMessageBox QLabel {"
        "  font-size: 18px;"
        "  font-weight: bold;"
        "  color: black;"
        "}"
        "QMessageBox QPushButton {"
        "  font-size: 18px;"
        "  font-weight: bold;"
        "  border: 2px solid #E0E0E0;"
        "  border-radius: 20px;"
        "  padding: 10px;"
        "  background-color: #0093D1;"
        "  color: white;"
        "}"
    );

    if (msgBox.exec() == QMessageBox::Ok) {
        emit pageRequested1();
    }
}
